<?php

/**
 * This is the model class for table "{{textbook_book}}".
 *
 * The followings are the available columns in table '{{textbook_book}}':
 * @property string $id
 * @property string $university_id
 * @property string $department_id
 * @property string $course_id
 * @property string $title
 * @property integer $user_id
 * @property string $isbn
 * @property string $description
 * @property integer $price
 * @property string $author
 * @property string $created
 * @property string $modified
 * @property string $image
 * @property string $thumbnail
 * @property string $status
 * @property string $google_api_json
 * @property string $phone
 * @property string $email
 *
 * The followings are the available model relations:
 * @property TextbookCourse $course
 * @property User $user
 */
class TextbookBook extends HActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TextbookBook the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{textbook_book}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('course_id, university_id, department_id, title, user_id, price, email, isbn', 'required'),
			array('user_id, price, status', 'numerical', 'integerOnly'=>true),
			array('course_id, university_id, department_id', 'length', 'max'=>11),
			array('course_id', 'numerical', 'integerOnly'=>true), // add 2012-06-02: only allow course is number
			array('title, author', 'length', 'max'=>100),
			array('isbn', 'length', 'max'=>15),
			array('phone', 'length', 'max'=>15),
			array('email', 'email'),
			array('thumbnail', 'length', 'max'=>255),
			array('image', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true),
			array('description, created, modified, google_api_json', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, course_id, university_id, department_id, title, user_id, isbn, description, google_api_json, price, phone, email, author, created, modified, image, thumbnail, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'course' => array(self::BELONGS_TO, 'TextbookCourse', 'course_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'university_id' => 'University',
			'department_id' => 'Department',
			'course_id' => 'Course',
			'title' => 'Title',
			'user_id' => 'User',
			'isbn' => 'ISBN',
			'description' => 'Description',
			'price' => 'Price',
			'author' => 'Author',
			'created' => 'Created',
			'modified' => 'Modified',
			'image' => 'Image',
			'thumbnail' => 'Thumbnail',
			'status' => 'Status',
			'phone' => 'Phone',
			'email' => 'Email',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('course_id',$this->course_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('isbn',$this->isbn,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('author',$this->author,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('thumbnail',$this->thumbnail,true);
		$criteria->compare('status',$this->status,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * This method is uses to get books of courses
	 */
	public function getBooksByCourses($courses, $search = '', $pages = null)
	{
		// Return empty array if does not exists courses
		if (!$courses) return array();
		
		$criteria = new CDbCriteria;
		$criteria->order = 'created DESC';
		
		if ($search) {
			$criteria->addSearchCondition('t.title', $search);
			$criteria->condition .= ' AND ';
		}
		
		$conditions = array();
		foreach ($courses as $index => $course) {
			$conditions[] = '(course_id=:course'.$index.')';
			$criteria->params[':course'.$index] = $course->id;
		}
		$criteria->condition .= '(' . implode(' OR ', $conditions) . ')';
		
		// Pagination
		if ($pages) {
			$count = self::model()->count($criteria);
			$pages->itemCount = $count;
			$pages->applyLimit($criteria);
		}
		
		return self::model()->findAll($criteria);
	}

	/**
	 * This method is used to get count books
	 */
	public function countBooks($course_id = null, $department_id = null)
	{
		if ($course_id) {
			return self::model()->countByAttributes(array(
				'course_id' => $course_id,
			));
		} else if ($department_id) {
			return self::model()->countByAttributes(array(
				'department_id' => $department_id,
			));
		}
	}

	/**
	 * This method is uses to get books
	 */
	public function getBooks($search = '', $university_id = null, $department_id = null, $pages = null)
	{
		$criteria = new CDbCriteria;
		$criteria->order = 'created DESC';
		
		if ($search) $criteria->addSearchCondition('t.title', $search);
		if ($university_id) $criteria->compare('university_id', $university_id);
		if ($department_id) $criteria->compare('department_id', $department_id);
		
		// Pagination
		if ($pages) {
			$count = self::model()->count($criteria);
			$pages->itemCount = $count;
			$pages->applyLimit($criteria);
		}
		
		return self::model()->findAll($criteria);
	}

	public function getThumb()
	{
		if ($this->image) return Yii::app()->getModule('textbooks')->strBookUploadPath . '/thumbs/' . $this->image;
		return $this->thumbnail;
	}
}